package com.thread.demo.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Create by xcb on 10:53 2022/3/2
 */
public class CallableTest {

    public static void main(String[] args) {

        class Mycallable implements Callable<Integer> {

            @Override
            public Integer call() throws Exception {
                System.out.println("当前线程名-" + Thread.currentThread().getName());
                int i = 0;
                for(; i < 5; i++) {
                    System.out.println("循环变量i的值：" + i);
                }
                return i;
            }
        }

        Mycallable mycallable = new Mycallable();
        FutureTask<Integer> futureTask = new FutureTask<>(mycallable);
        new Thread(futureTask, "线程名：有返回值的线程2").start();
        try {
            System.out.println("子线程的返回值：" + futureTask.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
